package httpclient;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HttpDemo {

    public static void main(String[] args) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost post = new HttpPost("https://link.bi.sensetime.com/api/v1/user");

        String time = String.valueOf(System.currentTimeMillis());
        //form-data的传参格式就是需要传输文件
        //创建一个MultipartEntityBuilder
        MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();

        //设置参数
        List list = new ArrayList<NameValuePair>();

//        list.add(new BasicNameValuePair("name", "张三"));
//        list.add(new BasicNameValuePair("type", "张三"));
//        list.add(new BasicNameValuePair("id", "222"));
        Map map = new HashMap();
        list.add(map);
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(list, "utf-8");


        ContentType contentType = ContentType.create("text/plain", Charset.forName("UTF-8"));
        //设置参数
        entityBuilder.addBinaryBody("avatarFile", new File("D:/ilongsay.png"));

        entityBuilder.addPart("jobNumbe", new StringBody("123", contentType));
        entityBuilder.addPart("mobile", new StringBody("110", contentType));
        entityBuilder.addPart("name", new StringBody("无敌大猩猩", contentType));
        entityBuilder.addPart("remark", new StringBody("备注信息是无敌", contentType));
        entityBuilder.addPart("departmentId", new StringBody("11", contentType));
        entityBuilder.addPart("areaCode", new StringBody("type", contentType));
        entityBuilder.addPart("entryTime", new StringBody("type", contentType));
        entityBuilder.addPart("birthday", new StringBody("type", contentType));
        entityBuilder.addPart("position", new StringBody("type", contentType));
        entityBuilder.addPart("location", new StringBody("type", contentType));
        entityBuilder.addPart("idNumber", new StringBody("type", contentType));
        entityBuilder.addPart("gender", new StringBody("type", contentType));
        entityBuilder.addPart("app_key", new StringBody("df3ea5d42d16c311", contentType));
        entityBuilder.addPart("sign", new StringBody(MD5Utils.md5(time, "7c588e605d0b26cf5b5861543ad1efaf"), contentType));

        entityBuilder.addPart("timestamp", new StringBody(time, contentType));

        post.setEntity(entityBuilder.build());

        //HttpResponse response = httpClient.execute(post);

        //响应模型
        CloseableHttpResponse response = null;

        try {
            //客户端发送get请求
            response = httpClient.execute(post);
            //从响应中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            System.out.println("响应状态为：" + response.getStatusLine());
            if (responseEntity != null) {
                System.out.println("响应内容长度为：" + responseEntity.getContentLength());
                System.out.println("响应内容为：" + EntityUtils.toString(responseEntity));
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

class Test {
    public static void main(String[] args) {
        System.out.println(String.valueOf(System.currentTimeMillis()));
    }
}

